package com.gc.business.service.impl;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gc.business.domain.ProjectItem;
import com.gc.common.support.Convert;
import com.gc.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import com.gc.business.mapper.UpkeepProjectMapper;
import com.gc.business.domain.UpkeepProject;
import com.gc.business.service.IUpkeepProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;

/**
 * 电梯保养项目 服务层实现
 * 
 * @author hruiyan
 * @date 2020-11-18
 */
@Slf4j
@Service
public class UpkeepProjectServiceImpl extends ServiceImpl<UpkeepProjectMapper, UpkeepProject> implements IUpkeepProjectService {

    @Override
    public List<UpkeepProject> list(UpkeepProject upkeepProject){
        LambdaQueryWrapper<UpkeepProject> wrapper = new LambdaQueryWrapper<>();
        if(upkeepProject != null){
            if(StringUtils.isNotEmpty(upkeepProject.getProjectId())){
                wrapper.eq(UpkeepProject::getProjectId, upkeepProject.getProjectId());
            }
            if(StringUtils.isNotEmpty(upkeepProject.getTemplateId())){
                wrapper.eq(UpkeepProject::getTemplateId, upkeepProject.getTemplateId());
            }
            if(StringUtils.isNotEmpty(upkeepProject.getProjectName())){
                wrapper.like(UpkeepProject::getProjectName, upkeepProject.getProjectName());
            }
            if(StringUtils.isNotEmpty(upkeepProject.getProjectRequire())){
                wrapper.eq(UpkeepProject::getProjectRequire, upkeepProject.getProjectRequire());
            }
            if(StringUtils.isNotEmpty(upkeepProject.getProjectType())){
                wrapper.eq(UpkeepProject::getProjectType, upkeepProject.getProjectType());
            }
            if(StringUtils.isNotEmpty(upkeepProject.getCreateBy())){
                wrapper.eq(UpkeepProject::getCreateBy, upkeepProject.getCreateBy());
            }
            if(StringUtils.isNotEmpty(upkeepProject.getCreateTime())){
                wrapper.eq(UpkeepProject::getCreateTime, upkeepProject.getCreateTime());
            }
            if(StringUtils.isNotEmpty(upkeepProject.getUpdateBy())){
                wrapper.eq(UpkeepProject::getUpdateBy, upkeepProject.getUpdateBy());
            }
            if(StringUtils.isNotEmpty(upkeepProject.getUpdateTime())){
                wrapper.eq(UpkeepProject::getUpdateTime, upkeepProject.getUpdateTime());
            }
            if(StringUtils.isNotEmpty(upkeepProject.getRemark())){
                wrapper.eq(UpkeepProject::getRemark, upkeepProject.getRemark());
            }
        }
        return baseMapper.selectList(wrapper);
    }


    /**
     * 保存项目清单列表
     * @param projectItem
     * @param templateId
     * @return
     */
    @Override
    public void saveProjectItemList(ProjectItem projectItem, Long templateId){
        //扶梯项目保养清单
        if(StringUtils.isNotEmpty(projectItem.getProjectName_0()) && StringUtils.isNotEmpty(projectItem.getProjectRequire_0())){
            Collection<String> projectNameArray_0 = CollUtil.newArrayList(Convert.toStrArray(projectItem.getProjectName_0()));
            Collection<String> projectRequireArray_0 = CollUtil.newArrayList(Convert.toStrArray(projectItem.getProjectRequire_0()));
            Map<String, String> zip_0 = CollUtil.zip(projectNameArray_0, projectRequireArray_0);

            UpkeepProject project = null;
            for(Map.Entry<String, String> entry : zip_0.entrySet()){
                System.out.println(entry.getKey()+"--->"+entry.getValue());
                project = new UpkeepProject();
                project.setProjectName(entry.getKey());
                project.setProjectRequire(entry.getValue());
                project.setProjectType("0");
                project.setTemplateId(templateId);
                baseMapper.insert(project);
            }
            return;
        }
        //保存直梯项目清单
        if(StringUtils.isNotEmpty(projectItem.getProjectName_1()) && StringUtils.isNotEmpty(projectItem.getProjectRequire_1())){
            Collection<String> projectNameArray_1 = CollUtil.newArrayList(Convert.toStrArray(projectItem.getProjectName_1()));
            Collection<String> projectRequireArray_1 = CollUtil.newArrayList(Convert.toStrArray(projectItem.getProjectRequire_1()));
            Map<String, String> zip_1 = CollUtil.zip(projectNameArray_1, projectRequireArray_1);

            UpkeepProject project = null;
            for(Map.Entry<String, String> entry : zip_1.entrySet()){
                System.out.println(entry.getKey()+"--->"+entry.getValue());
                project = new UpkeepProject();
                project.setProjectName(entry.getKey());
                project.setProjectRequire(entry.getValue());
                project.setProjectType("1");
                project.setTemplateId(templateId);
                baseMapper.insert(project);
            }
        }
        if(StringUtils.isNotEmpty(projectItem.getProjectName_2()) && StringUtils.isNotEmpty(projectItem.getProjectRequire_2())){
            Collection<String> projectNameArray_2 = CollUtil.newArrayList(Convert.toStrArray(projectItem.getProjectName_2()));
            Collection<String> projectRequireArray_2 = CollUtil.newArrayList(Convert.toStrArray(projectItem.getProjectRequire_2()));
            Map<String, String> zip_2 = CollUtil.zip(projectNameArray_2, projectRequireArray_2);

            UpkeepProject project = null;
            for(Map.Entry<String, String> entry : zip_2.entrySet()){
                System.out.println(entry.getKey()+"--->"+entry.getValue());
                project = new UpkeepProject();
                project.setProjectName(entry.getKey());
                project.setProjectRequire(entry.getValue());
                project.setProjectType("2");
                project.setTemplateId(templateId);
                baseMapper.insert(project);
            }
        }
        if(StringUtils.isNotEmpty(projectItem.getProjectName_3()) && StringUtils.isNotEmpty(projectItem.getProjectRequire_3())){
            Collection<String> projectNameArray_3 = CollUtil.newArrayList(Convert.toStrArray(projectItem.getProjectName_3()));
            Collection<String> projectRequireArray_3 = CollUtil.newArrayList(Convert.toStrArray(projectItem.getProjectRequire_3()));
            Map<String, String> zip_3 = CollUtil.zip(projectNameArray_3, projectRequireArray_3);

            UpkeepProject project = null;
            for(Map.Entry<String, String> entry : zip_3.entrySet()){
                System.out.println(entry.getKey()+"--->"+entry.getValue());
                project = new UpkeepProject();
                project.setProjectName(entry.getKey());
                project.setProjectRequire(entry.getValue());
                project.setProjectType("3");
                project.setTemplateId(templateId);
                baseMapper.insert(project);
            }
        }
        if(StringUtils.isNotEmpty(projectItem.getProjectName_4()) && StringUtils.isNotEmpty(projectItem.getProjectRequire_4())){
            Collection<String> projectNameArray_4 = CollUtil.newArrayList(Convert.toStrArray(projectItem.getProjectName_4()));
            Collection<String> projectRequireArray_4 = CollUtil.newArrayList(Convert.toStrArray(projectItem.getProjectRequire_4()));
            Map<String, String> zip_4 = CollUtil.zip(projectNameArray_4, projectRequireArray_4);

            UpkeepProject project = null;
            for(Map.Entry<String, String> entry : zip_4.entrySet()){
                System.out.println(entry.getKey()+"--->"+entry.getValue());
                project = new UpkeepProject();
                project.setProjectName(entry.getKey());
                project.setProjectRequire(entry.getValue());
                project.setProjectType("4");
                project.setTemplateId(templateId);
                baseMapper.insert(project);
            }
        }
    }

    /**
     * 根据模板ID删除
     * @param templateId
     * @return
     */
    @Override
    public int deleteByTemplateId(Long templateId){
        return baseMapper.deleteByTemplateId(templateId);
    }

}
